﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class job_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            BLL.jobs bll = new BLL.jobs();
            Model.jobs model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"type_id\":" + model.type_id);
            if (model.type_id == 1)
            {
                strTxt.Append(",\"type_name\":\"招聘\"");
            }
            else
            {
                strTxt.Append(",\"type_name\":\"求职\"");
            }
            strTxt.Append(",\"category_title\":\"" + new BLL.job_category().GetTitle(model.category_id) + "\"");
            strTxt.Append(",\"title\":\"" + model.title + "\"");
            string province_title = string.Empty;
            string city_title = string.Empty;
            if (model.province_id > 0)
            {
                province_title = new BLL.province().GetTitle(model.province_id);
            }
            if (model.city_id > 0)
            {
                city_title = new BLL.city().GetTitle(model.city_id);
            }
            strTxt.Append(",\"area\":\"" + province_title + city_title + "\"");
            strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\"");

            strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(model.content.ToString()) + "\"");
            strTxt.Append(",\"user_id\":" + model.user_id);
            if (model.user_id > 0)
            {
                string user_name = new BLL.users().GetUserName(model.user_id);
                string user_avatar = new BLL.users().GetAvatar(model.user_id);
                string mobile = new BLL.users().GetMobile(model.user_id);
                strTxt.Append(",\"user_name\":\"" + user_name + "\"");
                strTxt.Append(",\"mobile\":\"" + mobile + "\"");
                if (string.IsNullOrEmpty(user_avatar))
                {
                    strTxt.Append(",\"user_avatar\":\"../image/default_avatar.png\"");
                }
                else
                {
                    if (user_avatar.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"user_avatar\":\"http:\\/\\/" + user_avatar + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"user_avatar\":\"http:\\/\\/www.shicaiquanzi.com" + user_avatar + "\"");
                    }
                }
            }
            else
            {
                strTxt.Append(",\"user_name\":\"" + model.lianxiren + "\"");
                strTxt.Append(",\"user_avatar\":\"../image/default_avatar.png\"");
                strTxt.Append(",\"mobile\":\"" + model.mobile + "\"");
            }
            strTxt.Append(",\"hits\":" + model.hits);

            //推荐内容
            strTxt.Append(",\"job_list\":[");
            BLL.jobs bll2 = new BLL.jobs();
            DataSet ds = bll2.GetList(0, "type_id=" + model.type_id, "id desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"type_id\":" + dr["type_id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
            }
            strTxt.Append("]");

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}